/* The ProjectileAttack class is an ability that spawns one or more projectiles that damage targets on collision
 * The projectile(s) will be evenly spread across the angle specified in the editor, and will move as long as the range
 * of the ability permits.
 *
 * Author : Reinier
 */

class ProjectileAttack extends Ability {

public var hitEffect : GameObject;				/* The effect the projectiles should emit on death */
public var numberOfProjectiles : int = 1;		/* The number of projectiles the ability should spawn */
public var projectile : GameObject;				/* The projectile that should be spawned, note that this gameobject should have a Trigger-collider and a rigidbody to work properly */
public var projectileSpeed : int = 10;			/* The speed at which the projectiles should move */

/* The update function needs to call its parents update function since Unity does not do this */
function Update () {
	super.Update();
}

/* The Use function provides the basic functionality for the ability and returns true if the ability has been used,
 * or false if the ability is still on cooldown and thus cannot be used.
 *
 * This particular ability spawns an evenly spread out cone (or full circle, depending on the angle) of projectiles which
 * will detect impact and handle damage individually
 *
 * One projectile will always face forward, and such if only one projectile is specified to spawn, the angle is disregarded
 */
function Use() : boolean {
	/* checks if the currently counting cooldown is ready */
	if(currentCooldown <= 0) {
		
		if(projectile) {
			
			/* The counter and anglecounter variables are used later in the function to evenly spread out all projectiles */
			var counter : int = 1;
			var angleCounter : float = angle/numberOfProjectiles;
			
			for(i = 0; i < numberOfProjectiles; i++) {
				/* The projectiles are spawned at the users position and rotation */
				var newProjectile = Instantiate(projectile, gameObject.transform.localPosition, gameObject.transform.localRotation);

				/* The first projectile will always spawn in a direct forward angle */
				if(i == 0) {
					/* Do nothing */
				}
				else {
					/* Each projectile beyond the first needs to be rotated
					 * The angle is first divided between the number of projectiles to be spawned, this is the angleCounter instantiated earlier in this function
					 * Each uneven number of projectiles is sent at a negative angle to avoid the ability facing clockwise
					 * The anglecounter is multiplied by the counter, which is increased by one after each uneven number
					 */				
					var angularDirection : float;
					angularDirection = angleCounter * counter;
					
					/* Checks if the currently checked projectile is an uneven number */
					if(i%2) {
						/* If the projectile is an even number, the projectile is faced in a positive angle */
						newProjectile.transform.Rotate(0, angularDirection,0);
					}
					else {
						/* If the projectile is an uneven number, the projectile is faced in a negative angle */
						newProjectile.transform.Rotate(0, -angularDirection,0);
						/* After each uneven projectile, the counter is increased to face future projectiles at a more extreme angle */
						counter++;
					}
				}
				
				/* Each projectile should have a Projectile component to handle its own movement and collision detection */
				var projectileScript = newProjectile.AddComponent(Projectile);
				projectileScript.SetMaxDistance(range);
				projectileScript.SetOrigin(gameObject);
				projectileScript.SetSpeed(projectileSpeed);
				projectileScript.HitsMobs(hitsMobs);
				projectileScript.HitsPlayers(hitsPlayers);
				projectileScript.SetHitEffect(hitEffect);
				projectileScript.SetDamage(ApplyAttackModifier());
			}
		}
		else{
			Debug.Log("There is no projectile assigned to an attack, so it does nothing");
		}
		
		/* If the ability has been used succesfully, the function will return a true value */
		return true;
	}
	else{
		/* If the ability is still on cooldown, it cannot be used and will return a false value */
		return false;
	}
}

}